package com.mqtt.demo.test01;

import com.mqtt.demo.test02.MqttMessageGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/mqqt")
public class MqqtController {
    @Autowired
    private  MqttMessageGateway gateway;

    /**
     * 执行消息发布
     */
    @GetMapping("/projectmsg")
    @ResponseBody
    public  void pubProjectMsg(){
        Message<String> message = MessageBuilder.withPayload("ProjectMsg 新的笔记名称："+new Date())
                .setHeader(MqttHeaders.TOPIC, "ProjectMsg").build();
        gateway.sendMessage(message);
    }
    /**
     * 执行消息发布
     */
    @GetMapping("/managermsg")
    @ResponseBody
    public  void pubManagerMsg(){
        Message<String> message = MessageBuilder.withPayload("ManagerMsg，新的笔记名称："+new Date())
                .setHeader(MqttHeaders.TOPIC, "ManagerMsg").build();
        gateway.sendMessage(message);
    }
    /**
     * 执行消息发布
     */
    @GetMapping("/systemmsg/{guid}")

    public  void pubSystemMsg(@PathVariable String guid){
        Message<String> message = MessageBuilder.withPayload("SystemMsg，新的笔记名"+guid+"称："+new Date())
                .setHeader(MqttHeaders.TOPIC, "SystemMsg").build();
        gateway.sendMessage(message);
    }
}
